package cn.mygweb.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import cn.mygweb.web.controller.interceptor.OneInterceptor;
import cn.mygweb.web.controller.interceptor.TwoInterceptor;

/** 
* <p>Title: WebMvcConfigurer.java</p>
* <p>Description: 适配器配置</p>  
* @author gxb 
* @date 2019年5月23日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	//上传地址
	@Value("${file.upload.path}")
	private String uploadFilePath;
	
	//显示相对地址 
	@Value("${file.upload.path.relative}")
	private String uploadfFilePathRelative;
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		
		// 执行OneInterceptor并拦截所有的 /one/** 地址下面的请求
		// 注册一个拦截器多个路径，可以直接在后面写多个addPathPatterns(String... patters)
		// 同一个路径被多个拦截器拦截，按照编写的顺序执行拦截顺序
		registry.addInterceptor(new OneInterceptor()).addPathPatterns("/one/**");
		registry.addInterceptor(new TwoInterceptor()).addPathPatterns("/two/**").addPathPatterns("/one/**");
		
		WebMvcConfigurer.super.addInterceptors(registry);
	}

	//配置文件上传路径的映射关系
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {		
		registry.addResourceHandler(uploadfFilePathRelative).addResourceLocations("file:" + uploadFilePath);
	}
	
}
